package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.domain.UuId;
import com.example.blog.mapper.UuIdMapper;
import com.example.blog.service.UuIdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.Key;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class UuIdServiceImpl extends ServiceImpl<UuIdMapper, UuId> implements UuIdService {

    @Resource
    private UuIdMapper uuIdMapper;

    private final String HTTP_ = "http://www.lyzhnb.top/";

    @Override
    //根据传入的int值生成一定数量不重复的uuid
    public List<String> randomUUid(int length) {
        int i = 0;
        String key;
        List<String> keys = new ArrayList<>();
        UuId uuid = new UuId();
        do {
            key = UUID.randomUUID().toString();
            if (!(uuIdMapper.selectList(new LambdaQueryWrapper<UuId>().eq(UuId::getUuidValue, HTTP_.concat(key))) == null)) {
                keys.add(key);
                i++;
            }
        } while (i < length);
        return keys;
    }


}
